import { Message } from 'element-ui';
import validate from '@/utils/validate';

const { isEmpty } = validate;

export default function check (checkObj) {
  let checkRes = true;

  // 检查物料列表
  const validMateKeys = [
    { key: 'incode', name: '物料名称' },
    { key: 'currency', name: '币种' },
    { key: 'arrivalTime', name: '到货日期' }
  ];
  const mateLength = checkObj.materiels.length;
  for (let i = 0; i < mateLength; i++) {
    const line = i + 1;
    const mate = checkObj.materiels[i];
    // 合计
    const checkCountAll = isEmpty((+mate.buyNum) * (+mate.price));
    if (checkCountAll) {
      Message.error(`物料列表第${line}行 合计价格不能为空`);
      checkRes = false;
      break;
    }
    for (const item of validMateKeys) {
      const checkMid = isEmpty(mate[item.key]);
      if (checkMid) {
        Message.error(`物料列表第${line}行 ${item.name}不能为空`);
        checkRes = false;
        break;
      }
    }
  }

  // 检查标准输入
  const validKeys = [
    { key: 'applicant', name: '申请人' },
    { key: 'depart', name: '需求部门' },
    { key: 'supply', name: '供应商' },
    { key: 'costCenter', name: '成本中心' },
    { key: 'materiels', name: '物料' }
  ];
  for (const valid of validKeys) {
    const checkMid = isEmpty(checkObj[valid.key]);
    if (checkMid) {
      Message.error(`${valid.name}不能为空`);
      checkRes = false;
      break;
    }
  }

  return checkRes;
}
